package com.summer.controller;


import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.entity.TbApply;
import com.summer.entity.TbSign;
import com.summer.entity.TbUserinfo;
import com.summer.mapper.TbApplyMapper;
import com.summer.mapper.TbSignInMapper;
import com.summer.mapper.TbSignMapper;
import com.summer.utils.JwtToken;
import com.summer.utils.MyUtil;
import com.summer.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *测试完成20:45
 * @author summer502
 * @since 2021-03-31
 */
@CrossOrigin
@RestController
@RequestMapping("/tb-sign")
@Transactional
@Api(value = "TbSignController", description = "活动报名api")

public class TbSignController {
    /**
        private Integer signId;
        private Integer activityId;
        private String activityStatus;
        private Date create_time;
        private Date update_time;
        private Integer isDelete;
     */
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired(required =false)
    TbSignMapper tbSignMapper;

    /**
     * 超级管理员添加活动报名
     * @param activityId
     * @param activityStatus
     * @param Authorization
     * @return
     * @throws IOException
     * @throws TokenExpiredException
     */

    @ApiOperation("添加活动报名")
    @PostMapping("/teacher/addTbSign")
    @ResponseBody
    public ResponseEntity addTbSign(@RequestParam(required = false) int activityId,
                                     @RequestParam(required = false) String activityStatus,
                                     @RequestHeader String Authorization
    ) throws IOException, TokenExpiredException {
        ResponseEntity responseEntity;
        String token = Authorization;
        //parentRoleId
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo tbUserinfo = JwtToken.unsign(token, TbUserinfo.class);
        //用户角色，分微信用户和老师用户，1代表微信用户，2代表老师用户,默认为微信用户

        //根据user_jobNumber查询数据库是否有该用户存在，不存在则注册，存在则注册失败
        QueryWrapper<TbSign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId);
        TbSign tbSign = tbSignMapper.selectOne(queryWrapper);
        if (tbSign == null){
            responseEntity = ResponseEntity.ok();
            TbSign tbSign1 = new TbSign();
            tbSign1.setActivityStatus(activityStatus);
            tbSign1.setSign_user_id(tbUserinfo.getOpenid());
            tbSign1.setSign_user_name(tbUserinfo.getNickname());
            tbSignMapper.insert(tbSign1);
            responseEntity.putDataValue("sign_id",tbSign1.getSignId());
            responseEntity.putDataValue("msg","活动报名添加成功");
        }else {
            responseEntity = ResponseEntity.badRequest();
            responseEntity.putDataValue("msg","活动报名添加失败");
        }
        return responseEntity;
    }

    /**
     * 活动报名列表
     * @return
     * @throws IOException
     */
    @ApiOperation("活动报名列表")
    @PostMapping("/web/TbSignList")
    @ResponseBody
    public ResponseEntity TbSignList() throws IOException {
        ResponseEntity responseData;
        QueryWrapper<TbSign> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<TbSign> list = tbSignMapper.selectList(wrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("list",list);
        responseData.putDataValue("msg","成功");
        return responseData;
    }

    /**
     * 根据id删除活动报名
     * @param sign_id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id删除活动报名")
    @PutMapping("/web/deleteTbSign")
    @ResponseBody
    public ResponseEntity deleteTbSign(@RequestParam(required = true) Integer sign_id,@RequestHeader String Authorization)throws IOException{
        ResponseEntity responseEntity;
        String token=Authorization;
        if(flag=myUtil.judgeToken(token)){
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users=JwtToken.unsign(token,TbUserinfo.class);
        if(users==null){
            responseEntity=ResponseEntity.notFound();
            responseEntity.putDataValue("msg","用户不存在");
            return responseEntity;
        }else if(users.getRoleId()==1) {
            TbSign tbSign = tbSignMapper.selectById(sign_id);
            if(tbSign!=null){
                int is_delete = 1;
                tbSign.setIsDelete(is_delete);
                tbSignMapper.updateById(tbSign);
                responseEntity=ResponseEntity.ok();
                responseEntity.putDataValue("msg","删除成功");
                return responseEntity;
            }
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 根据applyId查询活动报名
     * @param sign_id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据sign_id查询活动报名")
    @PutMapping("/web/selectByTbSignId")
    @ResponseBody
    public ResponseEntity selectByTbSignId(@RequestParam(required = true) Integer sign_id, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getRoleId()==1) {
            responseEntity=ResponseEntity.ok();
            TbSign tbSign = tbSignMapper.selectById(sign_id);
            responseEntity.putDataValue("tbSign",tbSign);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 超级管理员修改活动报名
     * @param sign_id
     * @param activityId
     * @param activityStatus
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("超级管理员修改活动报名")
    @PutMapping("/web/updateTbSign")
    @ResponseBody
    public ResponseEntity updateTbSign(@RequestParam(required = true) int sign_id,
                                       @RequestParam(required = false) int activityId,
                                       @RequestParam(required = false) String activityStatus,
                                        @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getRoleId()!=1) {
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg", "没有权限");
            return responseEntity;

        } else {
            responseEntity = ResponseEntity.ok();
            TbSign tbSign1 = tbSignMapper.selectById(sign_id);
            tbSign1.setActivityStatus(activityStatus);
            tbSign1.setActivityId(activityId);
            tbSignMapper.updateById(tbSign1);
            responseEntity.putDataValue("msg", "活动报名修改成功");
            return responseEntity;
        }
    }

}
